<?php

require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

//get query dan variabel
$area = $_GET['area'];

//instansiasi fpdf
$pdf = new PDF_MC_Table('P','cm','A4');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.25,1,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(21,2, "STOK KENDARAAN CABANG", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->SetXY(0.8,3);

$sql = "SELECT area FROM gl_area WHERE id = '$area' ";
$cekdata = mysql_query($sql) or die(mysql_error());
$row_data = mysql_fetch_array($cekdata);
$pdf->Cell(8,3, "Area : ".$row_data['area'],0,0,'L');


//membuat kop tabel    
list($namaarea) = mysql_fetch_row(mysql_query("select area from gl_area where id='$area'"));
$pdf->SetFont('Arial','B',11);
$pdf->SetXY(0.5,5);
$pdf->Cell(20,1, "STOK KENDARAAN RENTAL CABANG ".$row_data['area'],1,2,'C');
$pdf->Cell(1,1, "No",1,0,'C');
$pdf->Cell(5,1, "Cabang",1,0,'C');
$pdf->Cell(3,1, "Coorporate",1,0,'C');
$pdf->Cell(2,1, "Retail",1,0,'C');
$pdf->Cell(3,1, "Tugas Kantor",1,0,'C');
$pdf->Cell(3,1, "OPS / UGC",1,0,'C');
$pdf->Cell(3,1, "Jumlah",1,0,'C');
$pdf->Ln();

$pdf->SetFont('Arial','',9);

//Isi
	$str = "select id, gudang from gl_gudang ";
	if($area != '') $str .= "where area = '$area'";
	$query = mysql_query($str) or die (mysql_error());
	$totdata = mysql_num_rows($query);
	if($totdata == '0'){
	echo "<tr><td colspan='7' align='center'><i>Data belum ada</i></td></tr>";
	}else{
	$no=1;
	while(list($idgudang, $namagudang) = mysql_fetch_row($query)){
	$pdf->SetX(0.5);
	$pdf->Cell(1,1, "$no",1,0,'C');
	$pdf->Cell(5,1, "$namagudang",1,0,'C');
	
	list($jumcorporate) = mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where cabang = '$idgudang' and unitstok='1'"));
	if($jumcorporate=='0') $jumcorporate='';
	$pdf->Cell(3,1, "$jumcorporate",1,0,'C');
	
	list($jumretail) = mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where cabang = '$idgudang' and unitstok='2'"));
	if($jumretail=='0') $jumretail='';
	$pdf->Cell(2,1, "$jumretail",1,0,'C');
	
	list($jumtugaskantor) = mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where cabang = '$idgudang' and unitstok='3'"));
	if($jumtugaskantor=='0') $jumtugaskantor='';
	
	$pdf->Cell(3,1, "$jumtugaskantor",1,0,'C');
	list($jumoperasional) = mysql_fetch_row(mysql_query("select count(*) from gl_masterkendaraan where cabang = '$idgudang' and unitstok='4'"));
	if($jumoperasional=='0') $jumoperasional='';
	$pdf->Cell(3,1, "$jumoperasional",1,0,'C');
	
	$jumtotal = $jumretail + $jumcorporate + $jumtugaskantor + $jumoperasional;
	if($jumtotal == 0) $jumtotal='';
	
	$pdf->Cell(3,1, "$jumtotal",1,1,'C');
	$no++;
	}
	}
	

$pdf->Output('Laporan Stok Kendaraan Rental Cabang '.$row_data['area'].'.pdf','D');


?>